﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

using LocalEvents.Business.Objects;
using System.Collections.Generic;
using System.Configuration;
using LocalEvents.Business;
using System.Workflow.Activities;
using System.Web.Script.Services;
using System.Reflection;

namespace LocalEvents.Portal
{
    /// <summary>
    /// Summary description for LocalEventsWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class LocalEventsWS : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public LocalEvent[] SearchLocalEvents(string location)
        {
            LocalEvent[] list = null;

            // Prepare a Dictionary object that will hold all the parameters to be
            // passed to workflow including the output parameters. e.g. LocalEventsData
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties.Add("Location", location);
            properties.Add("ErrorMessage", null);
            properties.Add("LocalEventsData", list);
            properties.Add("YahooKey", ConfigurationManager.AppSettings["YahooKey"]);
            properties.Add("ZventsKey", ConfigurationManager.AppSettings["ZventsKey"]);
            properties.Add("UpcomingKey", ConfigurationManager.AppSettings["UpcomingKey"]);
            properties.Add("EventfulAppKey", ConfigurationManager.AppSettings["EventfulAppKey"]);
            properties.Add("EventfulUser", ConfigurationManager.AppSettings["EventfulUser"]);
            properties.Add("EventfulPassword", ConfigurationManager.AppSettings["EventfulPassword"]);

            WorkflowHelper.ExecuteWorkflow(typeof(SearchWorkflow), properties);

            list = (LocalEvent[])properties["LocalEventsData"];

            if (list == null)
            {
                if (properties["ErrorMessage"] == null)
                    // No exception occured
                    // we just couldn't find any event there
                    throw new Exception("No local events found in the specified location");
                else
                    throw new Exception(properties["ErrorMessage"] as string);
            }


            TimeSpan duration = new TimeSpan(1, 0, 0);

            FieldInfo maxAge = HttpContext.Current.Response.Cache.GetType().GetField("_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);
            maxAge.SetValue(HttpContext.Current.Response.Cache, duration);

            Context.Response.Cache.SetCacheability(HttpCacheability.Public);
            Context.Response.Cache.SetExpires(DateTime.Now.Add(duration));
            Context.Response.Cache.SetMaxAge(duration);

            return list;
        }
    }
}
